import sys, os, pickle

class PyTimeManager():
	def __init__(self):
		self.usersSettingsFile = ".\settings.lib"
		self.usersClocksFile = ".\clocks.lib"
		self.identifierList = {'mon':0,'tue':1,
			'wed':2,'thu':3,'fri':4,'sat':5,'sun':6,
			'onc':7,'evd':8,'evm':9,'evy':10}

	def openUsersSettings(self):
		f = open(self.usersSettingsFile, 'rb')
		settings = pickle.load(f)
		f.close()
		return settings
		
	def writeUsersSettings(self, settings):
		f = open(self.usersSettingsFile, 'wb')
		pickle.dump(settings, f)
		f.close()
		return True
	
	def openUsersClocks(self):
		f = open(self.usersClocksFile, 'rb')
		clocks = pickle.load(f)
		f.close()
		return clocks
	
	def writeUsersClocks(self, allClocks):
		f = open(self.usersClocksFile, 'wb')
		pickle.dump(allClocks, f)
		f.close()
		return True
	
	def addOneClock(self, clock):
		allClocks = self.openUsersClocks()
		allClocks.append(clock)
		notUsed = self.writeUsersClocks(allClocks)
		return True
	
	def formatWeekIdentifier(self, wi):
		wi = wi.lower()
		try:
			return self.weekStrToNum[wi]
		except IndexError:
			raise IndexError("unknow identifier: %s" % wi)
	
	def formatWeekList(self, wil):
		back = []
		for i in wil:
			back.append(self.formatWeekIdentifier(i))
		return back
	
	def getDatesInMonth(self, year, month):
		if month != 2:
			if month > 7:
				if month % 2 == 1:
					return 30
				else:
					return 31
			else:
				if month % 2 == 1:
					return 31
				else:
					return 30
		else:
			if year % 100 != 0 and year % 4 == 0:
				return 29
			elif year % 400 == 0:
				return 29
			else:
				return 28


